package com.tenton.controller;

import com.tenton.MyException.DataNotExist;
import com.tenton.MyException.InsufficientQuantity;
import com.tenton.pojo.Sales;
import com.tenton.service.CommodityService;
import com.tenton.service.SalesService;
import com.tenton.service.StockService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Date: 2021/1/9
 * @Author: Tenton
 * @Description:
 */
@ApiIgnore
@Controller
@ApiOperation("员工商品销售管理")
@RequestMapping("/employee/flatform/sales")
public class SalesController {
    @Autowired
    SalesService salesService;
    @Autowired
    StockService stockService;
    @Autowired
    CommodityService commodityService;

    /**
     * 跳转到增加页面
     *
     * @return
     */
    @ApiOperation("跳转到增加页面")
    @GetMapping("/toAdd")
    public String toAdd() {
        return "/employee/sales/addSale";

    }

    /**
     * 先判断数据库有没有，没有就新增
     * 同时需要更新仓库中对应商品库存
     *
     * @param sale
     * @param request
     * @return
     */
    @ApiOperation("新增销售记录")
    @PostMapping("/insert")
    public String insert(Sales sale, HttpSession session, HttpServletRequest request) {
        try {
            salesService.insert(sale,session);
            return "redirect:/employee/flatform/sales/getAll";
        } catch (InsufficientQuantity insufficientQuantity) {
            request.setAttribute("msg", "商品库存不足，请重新输入！");
            return "employee/sales/addSale";
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "销售信息为空！");
            return "/employee/sales/addSale";
        }
    }

    /**
     * 获取所有销售信息
     *
     * @param model
     * @param pn
     * @return
     */
    @ApiOperation("获取所有销售信息")
    @GetMapping("/getAll")
    public String getAll_cus(ModelMap model,
                             @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        salesService.getAll(pn, model);
        return "/employee/sales/getSalesList";
    }

    /**
     * 查询销售记录
     *
     * @param id
     * @param model
     * @return
     */
    @ApiOperation("通过Id查询销售记录")
    @GetMapping("/getSales")
    public String getSales(int id, Model model) {
        salesService.selectByPrimaryKey(id, model);
        return "/employee/sales/getSale";
    }

    /**
     * 查询商品信息
     *
     * @param comId
     * @param model
     * @return
     */
    @ApiOperation("通过comId查询商品信息")
    @GetMapping("/getCommodityByComId")
    public String getCommodityByComId(int comId, Model model, HttpServletRequest request) {
        try {
            commodityService.getCommodityByComId(comId, model);
        } catch (DataNotExist dataNotExist) {
            request.setAttribute("msg", "商品信息不存在！");
        }
        return "employee/sales/addSale";
    }

    /**
     * 商品名称模糊查询
     *
     * @param model
     * @param comName
     * @param pn
     * @return
     */
    @ApiOperation("商品名称模糊查询")
    @GetMapping("getByParams")
    public String getByParams(ModelMap model,
                              @ApiParam("商品名称") @RequestParam(value = "comName", required = false) String comName,
                              @ApiParam("页码") @RequestParam(defaultValue = "1", required = true, value = "pn") Integer pn
    ) {
        salesService.getByParams(comName, pn, model);
        return "/employee/sales/getSaleByParams";

    }
}
